﻿using UnityEngine;
using System.Collections;
using System;

public class GameController : MonoBehaviour {

    public int columnNum = 8;
    public int rowNum = 8;
    public GameObject pot1,cat,pot2;
    private ArrayList pathArray;
    public bool started = false;
    public GameObject startScreen;
    public GameObject victory;
    public GameObject failed;
    public GameObject replay;
    public GameObject content;

    // Use this for initialization
    void Start () {
        
    }

    void Awake()
    {
        Debug.Log(gameObject.name);
        content = gameObject.transform.FindChild("content").gameObject;
    }

    public void InitGame()
    {
        pathArray = new ArrayList();
        for (int rowIndex = 0; rowIndex < rowNum; rowIndex++)
        {
            ArrayList subPathArray = new ArrayList();
            for (int columnIndex = 0; columnIndex < columnNum; columnIndex++)
            {
                Item item = CreatePot(pot1, columnIndex, rowIndex);
                subPathArray.Add(item);
            }
            pathArray.Add(subPathArray);
        }
        MoveCat(4, 4);
    }

    public void DestoryInit()
    {
        for (int i = 0; i < content.transform.childCount; i++)
        {
            Destroy(content.transform.GetChild(i).gameObject);
        }
        pathArray.Clear();
    }

    public ArrayList GetCatSteps()
    {
        Cat c = cat.GetComponent<Cat>();
        Vector2 v = new Vector2();
        ArrayList steps = new ArrayList();

        if (c.rowIndex % 2 == 0)
        {
            //top steps
            v.x = c.rowIndex + 1;
            v.y = c.columnIndex;
            steps.Add(v);

            v.x = c.rowIndex + 1;
            v.y = c.columnIndex + 1;
            steps.Add(v);

            v.x = c.rowIndex + 1;
            v.y = c.columnIndex + 2;
            steps.Add(v);

            //bottom
            v.x = c.rowIndex - 1;
            v.y = c.columnIndex;
            steps.Add(v);

            v.x = c.rowIndex - 1;
            v.y = c.columnIndex + 1;
            steps.Add(v);

            v.x = c.rowIndex - 1;
            v.y = c.columnIndex + 2;
            steps.Add(v);
        }
        else
        {
            //top steps
            v.x = c.rowIndex + 1;
            v.y = c.columnIndex-2;
            steps.Add(v);

            v.x = c.rowIndex + 1;
            v.y = c.columnIndex - 1;
            steps.Add(v);

            v.x = c.rowIndex + 1;
            v.y = c.columnIndex ;
            steps.Add(v);

            //bottom
            v.x = c.rowIndex - 1;
            v.y = c.columnIndex-2;
            steps.Add(v);

            v.x = c.rowIndex - 1;
            v.y = c.columnIndex - 1;
            steps.Add(v);

            v.x = c.rowIndex - 1;
            v.y = c.columnIndex;
            steps.Add(v);
        }

        //near steps
        v.x = c.rowIndex;
        v.y = c.columnIndex - 1;
        steps.Add(v);

        v.x = c.rowIndex ;
        v.y = c.columnIndex + 1;
        steps.Add(v);

        ArrayList finalSteps = new ArrayList();
        for (int i = 0; i < steps.Count; i++)
        {
            Vector2 vector2 = (Vector2)steps[i];
            if (vector2.x < 0 || vector2.x > rowNum - 1 || vector2.y < 0 || vector2.y > columnNum - 1)
            {
                continue;
            }
            if (!GetPot((int)vector2.y, (int)vector2.x).movable)
            {
                continue;
            }
            finalSteps.Add(vector2);
        }

        return finalSteps;

    }

    public Item GetPot(int columnIndex, int rowIndex)
    {
        ArrayList o = pathArray[rowIndex] as ArrayList;
        Item item = o[columnIndex] as Item;
        return item;
    }

    public void MoveCat(int columnIndex, int rowIndex)
    {
        Cat c = cat.GetComponent<Cat>();
        c.game = this;
        c.MoveCat(columnIndex, rowIndex);
    }

    public Item CreatePot(GameObject pot,int columnIndex, int rowIndex)
    {
        GameObject o = Instantiate(pot); //拷贝pot的实例
        o.transform.parent = content.transform; //实例都放在GameController下面
        Item item = o.GetComponent<Item>();
        item.MoveItem(columnIndex, rowIndex);
        item.game = this;
        return item;
    }
	// Update is called once per frame
	void Update () {
	
	}

}
